-- Copyright (c) 2025 Huawei Technologies Co., Ltd.
-- openUBMC is licensed under Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at:
--         http://license.coscl.org.cn/MulanPSL2
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
-- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
-- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-- See the Mulan PSL v2 for more details.
--
-- Create: 2025-7-21
-- Description: remote single board object.
local class = require 'mc.class'
local log = require 'mc.logging'

local module = class{}

function module:ctor(mdb_obj, name, restart_client_sig)
    self.name = name
    self.id = mdb_obj.Id
    self.mdb_obj = mdb_obj
    self.service_addr = nil
    self.restart_client_sig = restart_client_sig
end

function module:initialize()
    -- 创建对于IP地址的监听，发生变更后需要重新建立客户端
    self.mdb_obj.property_changed:on(function(name, value, sender)
        if name ~= 'Address' and name ~= 'Port' then
            return
        end
        self.service_addr = string.format('tcp://peer:ipv4:%s:%s:1', self.mdb_obj.Address, self.mdb_obj.Port)
        log:notice('[%s] client address change to [%s]', self.name, self.service_addr)
        self.restart_client_sig:emit(self.name)
    end)
    self.service_addr = string.format('tcp://peer:ipv4:%s:%s:1', self.mdb_obj.Address, self.mdb_obj.Port)
    log:notice('[%s] client address [%s]', self.name, self.service_addr)
end

return module